home *** CD-ROM | disk | FTP | other *** search
/ PCMania 24 / PCMania CD24.nrg / juego24 / tepract.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-07  |  7.7 KB  |  276 lines

  1. /****************************************************************************/
  2. /*                     PRUEBA DEL TECLADO MULTIFUNCION                      */
  3. /*                                                                          */
  4. /*      por: José Antonio Acedo Martín-Grande, miembro de "GOLDY GAMES"     */
  5. /*                                                                          */
  6. /*                      TEL: (91) 6 11 72 71 (MADRID)                       */
  7. /****************************************************************************/
  8.  
  9. /**************************** FICHEROS A INCLUIR  ***************************/
  10.  
  11. #include <dos.h>
  12. #include <bios.h>
  13. #include <conio.h>
  14. #include <stdio.h>
  15. #include <alloc.h>
  16. #include <stdlib.h>
  17.  
  18. /******************************* DEFINICIONES *******************************/
  19.  
  20. #define ALFANUMERICO    3
  21. #define GRAFICO        19
  22. #define ANCHO          37
  23. #define ALTO           35
  24.  
  25. /*********************** DECLARACIONES DE FUNCIONES *************************/
  26.  
  27.        void  DIBUJA_BLOQUE_CUT(int *, char *, char *);
  28.        void  DIBUJA_BLOQUE    (int *, char *, char *);
  29.        void  VUELCA_PANTALLA(int,int, int,int, int,int, char *);
  30.        void  SET_VECTORES(volatile char *);
  31.        void  UNSET_VECTORES(void);
  32.        void  espera_soltar_tecla(void);
  33.        int   espera_pulsar_tecla(void);
  34.            void  asigna_modo_video(char);
  35.        void  lee_ficheros(const char *, char *);
  36.            void  descomprime_dibujo(char *);
  37.            void  asigna_rgb(char *);
  38.            void  asigna_memoria(void);
  39.            void  libera_memoria(void);
  40.            void  sin_memoria(void);
  41.            void  error_fichero(void);
  42.            void  limpia_teclado(void);
  43.            void  salir_al_dos(void);
  44.  
  45. /*************************** VARIABLES GLOBALES /****************************/
  46.  
  47.        char *dir_carga_dibujos;
  48.        char *dir_dibujo1;
  49.        char *dir_dibujo2;
  50.        char *dir_zona_pantalla;
  51.            FILE *handle_dibujos;
  52. const       char  dibujo1[] = { "tepract1.pcx" };
  53. const       char  dibujo2[] = { "tepract2.pcx" };
  54. volatile   char  mapa_teclado[256];
  55.        int   pelota [6] = { 0,0, 140,82,  37, 35 };
  56.        int   fondo  [6] = { 0,0,   0, 0, 320,200 };
  57.  
  58. /********************************* PROGRAMA *********************************/
  59.  
  60. void main(void)
  61. {
  62.     int key_der=0, key_izq=0, key_arr=0, key_aba=0, x, y;
  63.  
  64.     asigna_memoria();
  65.     asigna_modo_video(GRAFICO);
  66.     lee_ficheros(dibujo1, dir_dibujo1);
  67.     lee_ficheros(dibujo2, dir_dibujo2);
  68.     SET_VECTORES(mapa_teclado);
  69.  
  70.     limpia_teclado();
  71.     gotoxy(10,10); printf("DEFINE UNA TECLA PARA:");
  72.  
  73.     gotoxy(17,12); printf(" DERECHA ");
  74.     while(key_der==0       ||
  75.           key_der==key_izq ||
  76.           key_der==key_arr ||
  77.           key_der==key_aba)
  78.     {
  79.       key_der=espera_pulsar_tecla();
  80.     }
  81.  
  82.     gotoxy(17,12); printf("         ");
  83.     espera_soltar_tecla();
  84.  
  85.     gotoxy(17,12); printf("IZQUIERDA");
  86.     while(key_izq==0       ||
  87.           key_izq==key_der ||
  88.           key_izq==key_arr ||
  89.           key_izq==key_aba)
  90.     {
  91.       key_izq=espera_pulsar_tecla();
  92.     }
  93.  
  94.     gotoxy(17,12); printf("         ");
  95.     espera_soltar_tecla();
  96.  
  97.     gotoxy(17,12); printf(" ARRIBA  ");
  98.     while(key_arr==0       ||
  99.           key_arr==key_der ||
  100.           key_arr==key_izq ||
  101.           key_arr==key_aba)
  102.     {
  103.       key_arr=espera_pulsar_tecla();
  104.     }
  105.  
  106.     gotoxy(17,12); printf("         ");
  107.     espera_soltar_tecla();
  108.  
  109.     gotoxy(17,12); printf("  ABAJO  ");
  110.     while(key_aba==0       ||
  111.           key_aba==key_der ||
  112.           key_aba==key_izq ||
  113.           key_aba==key_arr)
  114.     {
  115.       key_aba=espera_pulsar_tecla();
  116.     }
  117.  
  118.     gotoxy(17,12); printf("         ");
  119.     espera_soltar_tecla();
  120.  
  121.     while(mapa_teclado[1]==0) {
  122.       x=pelota[2]; y=pelota[3];
  123.       if(mapa_teclado[key_der]==1) { x+=8; if(x>=    320) x=0-ANCHO; }
  124.       if(mapa_teclado[key_izq]==1) { x-=8; if(x< 0-ANCHO) x=    319; }
  125.       if(mapa_teclado[key_arr]==1) { y-=8; if(y< 0- ALTO) y=    199; }
  126.       if(mapa_teclado[key_aba]==1) { y+=8; if(y>=    200) y=0- ALTO; }
  127.       pelota[2]=x; pelota[3]=y;
  128.       DIBUJA_BLOQUE    (fondo,  dir_dibujo2, dir_zona_pantalla);
  129.       DIBUJA_BLOQUE_CUT(pelota, dir_dibujo1, dir_zona_pantalla);
  130.       VUELCA_PANTALLA(0,0, 0,0, 320,200, dir_zona_pantalla);
  131.     }
  132.     salir_al_dos();
  133. }
  134.  
  135. void espera_soltar_tecla(void)
  136. {
  137.     int c, a;
  138.  
  139.     while(1==1)
  140.     {
  141.       a=0;
  142.       for(c=1; c<256; c++)
  143.       {
  144.         a=a|mapa_teclado[c];
  145.       }
  146.       if(a==0) break;
  147.     }
  148. }
  149.  
  150. int espera_pulsar_tecla(void)
  151. {
  152.     int c;
  153.  
  154.     while(1==1)
  155.     {
  156.       for(c=1; c<256; c++)
  157.       {
  158.         if(mapa_teclado[c]==1) return c;
  159.       }
  160.     }
  161. }
  162.  
  163. void salir_al_dos(void)
  164. {
  165.         asigna_modo_video(ALFANUMERICO);
  166.         libera_memoria();
  167.         UNSET_VECTORES();
  168.         exit(0);
  169. }
  170.  
  171. void limpia_teclado(void)
  172. {
  173.         int n;
  174.  
  175.         for(n=0; n<256; n++) { mapa_teclado[n]=0; }
  176. }
  177.  
  178. void asigna_memoria(void)
  179. {
  180.         if((dir_carga_dibujos=(char *)malloc(320*200))==NULL) sin_memoria();
  181.         if((dir_dibujo1      =(char *)malloc(320*200))==NULL) sin_memoria();
  182.     if((dir_dibujo2      =(char *)malloc(320*200))==NULL) sin_memoria();
  183.     if((dir_zona_pantalla=(char *)malloc(320*200))==NULL) sin_memoria();
  184. }
  185.  
  186. void libera_memoria(void)
  187. {
  188.         free(dir_carga_dibujos);
  189.         free(dir_dibujo1);
  190.     free(dir_dibujo2);
  191.     free(dir_zona_pantalla);
  192. }
  193.  
  194. void sin_memoria(void)
  195. {
  196.         asigna_modo_video(ALFANUMERICO);
  197.         libera_memoria();
  198.         printf("No hay suficiente memoria. Libere programas residentes.");
  199.         exit(1);
  200. }
  201.  
  202. void error_fichero(void)
  203. {
  204.         asigna_modo_video(ALFANUMERICO);
  205.         libera_memoria();
  206.         printf("Error leyendo fichero .PCX");
  207.         exit(1);
  208. }
  209.  
  210. void asigna_modo_video(char modo)     /* asigna el modo de vídeo indicado */
  211. {                                     /* en la variable "modo"            */
  212.         union REGS ent, sal;
  213.  
  214.         ent.h.al = modo;
  215.         ent.h.ah = 0;
  216.     int86(16, &ent, &sal);        /* función para asignar el modo de video */
  217. }
  218.  
  219. void lee_ficheros(const char *file, char *dir_dibujo)
  220. {
  221.         char *dir;
  222.  
  223.         dir=dir_carga_dibujos;
  224.         if((handle_dibujos = fopen(file, "rb"))==NULL) error_fichero();
  225.         while(!feof(handle_dibujos)) { *dir++=getc(handle_dibujos); }
  226.         fclose(handle_dibujos);
  227.         descomprime_dibujo(dir_dibujo);
  228. }
  229.  
  230. void descomprime_dibujo(char *dir_escritura)
  231. {
  232.         unsigned char  byte;
  233.                  char *dir_lectura;
  234.                  int   columnas, filas, contador;
  235.  
  236.     dir_lectura=dir_carga_dibujos+128;     /* inicio del dibujo comprimido */
  237.  
  238.         for(filas=200; filas>0; filas--) {
  239.           columnas=320;
  240.           dir_escritura-=columnas; dir_escritura+=320;
  241.           while(columnas>0) {
  242.             byte=(unsigned)*dir_lectura++;
  243.             if(byte<=192) { *dir_escritura++=byte; columnas--; }
  244.             else {
  245.               contador=byte&63; byte=*dir_lectura++;
  246.               for(;contador>0;contador--) { *dir_escritura++=byte;columnas--; }
  247.             }
  248.           }
  249.         }
  250.     asigna_rgb(dir_lectura+1);       /* remapea los 256 colores del dibujo */
  251. }
  252.  
  253. void asigna_rgb(char *dir_lectura)
  254. {
  255.         struct   SREGS  seg;
  256.         union     REGS  ent, sal;
  257.         unsigned  int   n;
  258.         long      int   dir;
  259.                   char *dir_col;
  260.  
  261.     dir_col=dir_lectura;                      /* divide entre  */
  262.     for(n=256*3; n>0; n--) {                  /* 4 los colores */
  263.           *dir_col=*dir_col >> 2; dir_col++;
  264.         }
  265.  
  266.     dir   =(long)dir_lectura;                 /* obtiene el segmento     */
  267.     seg.es=(int)(dir >> 16);                  /* donde estan los colores */
  268.  
  269.         ent.h.al =  18;
  270.         ent.h.ah =  16;
  271.         ent.x.bx =   0;
  272.         ent.x.cx = 256;
  273.     ent.x.dx = (int)dir_lectura;       /* offset de los colores            */
  274.     int86x(0x10, &ent, &sal, &seg);    /* función para asignar los colores */
  275. }
  276.